package alvarodelrosal.chat.server.modelo.chataction;

import alvarodelrosal.chat.server.infraestructura.ChatPersistenceManager;
import alvarodelrosal.chat.server.modelo.ChatConnection;
import alvarodelrosal.chat.server.modelo.ChatMessage;
import java.util.List;

public class ChatConversation implements ChatAction {

    @Override
    public String getName() {
        return "Conversation";
    }

    @Override
    public boolean needsAdminPrivileges() {
        return false;
    }

    @Override
    public boolean needsLogin() {
        return true;
    }

    @Override
    public String doAction(ChatConnection connection, List<String> parameters) {
        StringBuilder builder = new StringBuilder("Conversation<:@:>");
        
        List<ChatMessage> messages = ChatPersistenceManager.getPersistenceManager().
                getMessagesFrom(Integer.parseInt(parameters.get(0)));
        
        builder.append(parameters.get(0));
        
        for (ChatMessage message : messages) {
            builder.append("<:@:>");
            builder.append(message.getUser().getUserId());
            builder.append("<:@:>");
            builder.append(message.getText());
        }
        
        return builder.toString();
    }
    
}
